package com.utonw.carloan.fragment.applyloan;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Switch;
import android.widget.TextView;

import com.sensetime.library.finance.ocr.card.IdCardInfo;
import com.utonw.carloan.R;
import com.utonw.carloan.bean.applyloan.ContractBean;
import com.utonw.carloan.fragment.BaseFragment;
import com.utonw.carloan.function.GlobalLoanFunction;
import com.utonw.carloan.global.ReleaseData;
import com.utonw.carloan.utils.ChooseBarUtils;
import com.utonw.carloan.utils.UIUtils;
import com.utonw.carloan.utils.ViewUtils;

import java.util.ArrayList;

/**
 * Created by SEELE on 2017/3/22.
 */

/**
 * 申请贷款流程7：联系人
 */
public class LoanStepSevenFragment extends BaseFragment {
    //新增联系人按钮
    private LinearLayout mAddContract;
    //追加联系人的容器
    private LinearLayout mAddContractView;
    //默认联系人布局
    private LinearLayout mContractOri;
    //默认联系人 关系 字段
    private TextView mRelative;
    //默认联系人 姓名 字段
    private TextView mName;
    //默认联系人联系 电话 字段
    private TextView mTel;
    //默认联系人 是否知道贷款
    private Switch mIsKnow;
    //关系 数据源
    private ArrayList<String> mRelativeData;

    @Override
    public int setLayout() {
        return R.layout.fragment_applyloan_step_seven;
    }

    @Override
    public void initView(View view) {
        mAddContract = (LinearLayout) mActivity.findViewById(R.id.ll_add_contract);
        mAddContractView = (LinearLayout) mActivity.findViewById(R.id.ll_frame_add_contract);
        mContractOri = (LinearLayout) mActivity.findViewById(R.id.lianxirenxinxi_ori);

        //默认联系人关系字段
        mRelative = (TextView) mContractOri.findViewById(R.id.choosebar_action_extra_contract).findViewById(R.id.tv_choosebar_to_show);

        //默认联系人姓名字段
        mName = (EditText) mContractOri.findViewById(R.id.et_contract_name);

        //默认联系人电话字段
        mTel = (EditText) mContractOri.findViewById(R.id.et_contract_tel);

        //默认联系人 是否知道贷款
        mIsKnow = (Switch) mContractOri.findViewById(R.id.sw_contract_isknow);
        //隐藏默认联系人的删除功能
        mContractOri.findViewById(R.id.ll_delete_extra_contract).setVisibility(View.GONE);
    }

    @Override
    public void initData() {

        //初始化数据源
        mRelativeData = new ArrayList<>();
        mRelativeData.add("父母");
        mRelativeData.add("亲属");
        mRelativeData.add("同事");
        mRelativeData.add("朋友");

        //将默认联系人字段添加到校验集合
        setCheckViews(R.string.apply_page_seven_relative,mRelative);
        setCheckViews(R.string.apply_page_seven_name,mName);
        setCheckViews(R.string.apply_page_seven_tel,mTel);

        //初始化PickerView选择器
        final GlobalLoanFunction globalLoanFunction = new GlobalLoanFunction();
        //设置ChooseBar标题
        ChooseBarUtils titleUtils = new ChooseBarUtils(getHoldingActivity()) {
            @Override
            protected void setChooseBarClickListener(int view) {

            }

            @Override
            protected void setChooseBarClickListener(int view, TextView toShow) {

            }

            @Override
            protected void getCheckedViewListener(int includeId, int sTitle, View toCheck) {
                setCheckViews(sTitle,toCheck);
            }

            @Override
            public void onToggleButtonListener(int buttonView, boolean isChecked) {

            }

            @Override
            public void getEditTextWithTipsListener(int includeId, EditText tempEt1) {

            }

            @Override
            public void onRadioButtonListener(RadioButton rMale, RadioButton rFemale) {

            }

            @Override
            public void onImageChooseBarListener(int view, ImageView mIndicator, TextView mInstruction, int sTitle) {

            }

            @Override
            public void getChangeWordsEditTextListener(int includeId, EditText tempEt1, int sTitle) {

            }

            @Override
            public void setVerificationListener() {

            }
        };
        /**
         * 取消原本界面，改用默认布局
         */
//        titleUtils.initChooseBar(-1, R.id.guanxi, R.string.apply_page_seven_relative);
//        titleUtils.initChooseBar(-1, R.id.lianxirenxingming, R.string.apply_page_seven_name, ChooseBarUtils.ResultState.CHANGE_WORDS, "请填写姓名");
//        titleUtils.initChooseBar(-1, R.id.lianxidianhua, R.string.apply_page_seven_tel, ChooseBarUtils.ResultState.CHANGE_WORDS, "请输入联系电话");
//        titleUtils.initChooseBar(-1, R.id.shifouzhixiaodaikuan, R.string.apply_page_seven_is_known, ChooseBarUtils.ResultState.TOGGLE_BUTTON, null);
        titleUtils.initChooseBar("2","1","mobile",R.id.shoujihaoma, R.string.apply_page_two_phone_num, ChooseBarUtils.ResultState.CHANGE_WORDS, "请输入");
        titleUtils.initChooseBar("0","0","?",-1,R.id.shoujichaxunmima, R.string.apply_page_two_phone_pwd, ChooseBarUtils.ResultState.CHANGE_WORDS, "请输入");
        titleUtils.initChooseBar("0","0","?",-1, R.id.shoujiyanzhengma, R.string.apply_page_two_verification_code, ChooseBarUtils.ResultState.VERIFICATION, null);


        //默认联系人的“关系” 点击事件
        mRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                globalLoanFunction.setTempData(mRelativeData, mRelative).oriPickerView(getContext());
            }
        });

        //增加联系人的点击事件
        mAddContract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAddContractView.getChildCount() < 4) {
                    //生成追加的联系人界面
                    final View view = ViewUtils.addExtraContractView(R.layout.item_contract_extra);
                    //删除该额外联系人
                    view.findViewById(R.id.ll_delete_extra_contract).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mAddContractView.removeView(view);
                        }
                    });

                    //关系 点击监听
                    view.findViewById(R.id.choosebar_action_extra_contract).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            globalLoanFunction.setTempData(mRelativeData, (TextView) view.findViewById(R.id.choosebar_action_extra_contract).findViewById(R.id.tv_choosebar_to_show)).oriPickerView(getContext());
                        }
                    });


                    mAddContractView.addView(view);

                } else {
                    UIUtils.showShortToast("最多只能添加4个额外联系人");
                }


            }
        });


    }

    @Override
    protected void getIdcardFrontInfo(IdCardInfo idCardInfo) {

    }

    @Override
    protected void getIdcardBackInfo(IdCardInfo idCardInfo) {

    }

    @Override
    protected void getBankcardInfo(Intent cardData) {

    }

    @Override
    protected void getCreditcardInfo(Intent cardData) {

    }

    public ArrayList<ContractBean> getFinalData() {

        ContractBean contractBeanOri = new ContractBean();
        //获取默认联系人的 关系 字段
        contractBeanOri.setRelation(mRelative.getText().toString().
                equals(getString(R.string.apply_page_begin_please_choose))?
                null:mRelative.getText().toString());
        contractBeanOri.setContactsName(mName.getText().toString().equals("")?null:mName.getText().toString());
        contractBeanOri.setContactsMobile(mTel.getText().toString().equals("")?null:mTel.getText().toString());
        contractBeanOri.setIsKnow(mIsKnow.isChecked()?1+"":0+"");

        ArrayList<ContractBean> tempList = new ArrayList<>();

        for (int i = 0; i < mAddContractView.getChildCount(); i++) {
            //获取额外联系人的关系字段
            TextView tempRelative = (TextView) mAddContractView.getChildAt(i)
                    .findViewById(R.id.choosebar_action_extra_contract).
                            findViewById(R.id.tv_choosebar_to_show);
            ReleaseData.tempUnused.add(tempRelative);

            //获取额外联系人的姓名
            EditText tempName = (EditText) mAddContractView.getChildAt(i)
                    .findViewById(R.id.et_contract_name);
            ReleaseData.tempUnused.add(tempName);

            //获取额外联系人的联系电话
            EditText tempTel = (EditText) mAddContractView.getChildAt(i)
                    .findViewById(R.id.et_contract_tel);
            ReleaseData.tempUnused.add(tempTel);

            //获取额外联系人的 是否知道贷款
            Switch tempIsKnow = (Switch) mAddContractView.getChildAt(i)
                    .findViewById(R.id.sw_contract_isknow);
            ReleaseData.tempUnused.add(tempIsKnow);

            ContractBean contractBeanExtra = new ContractBean();
            //设置额外联系人的关系
            contractBeanExtra.setRelation(tempRelative.getText().toString().
                    equals(getString(R.string.apply_page_begin_please_choose))?
                    null:tempRelative.getText().toString());
            //设置额外联系人的姓名
            contractBeanExtra.setContactsName(tempName.getText().toString().equals("")?null:tempName.getText().toString());
            //设置额外联系人的联系电话
            contractBeanExtra.setContactsMobile(tempTel.getText().toString().equals("")?null:tempTel.getText().toString());
            //设置额外联系人 是否知道贷款
            contractBeanExtra.setIsKnow(tempIsKnow.isChecked()?1+"":0+"");

            tempList.add(contractBeanExtra);
        }

        ReleaseData.getAllContractBean.add(contractBeanOri);

        if (0 != tempList.size()) {
            ReleaseData.getAllContractBean.addAll(tempList);
            ReleaseData.tempUnused.add(tempList);
        } else {
            tempList = null;
        }

        return ReleaseData.getAllContractBean;

       // LogUtil.d(new Gson().toJson(ReleaseData.getAllContractBean));

    }

}
